﻿using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Atlanchim.Crm.Mui.Pages.Documents
{
    public class PageEditDocumentModel : AbstractPageEditModel
    {
        public Document Document { get; set; }

        public override void InnerLoad()
        {
            if (!string.IsNullOrEmpty(this.Fragment))
            {
                // Récupération des Ids
                int idDocument = 0;
                int.TryParse(this.DicFragments["IdDocument"], out idDocument);

                int idSociete = 0;
                if (this.DicFragments.ContainsKey("IdSociete"))
                {
                    int.TryParse(this.DicFragments["IdSociete"], out idSociete);
                }

                // Chargement ou création de l'entité
                if (idDocument > 0)
                {
                    this.Document = this._pageContext
                                        .Documents
                                        .Where(document => document.Id == idDocument)
                                        .FirstOrDefault();
                }
                else
                {
                    this.Document = new Document();
                    this.Document.IdSociete = idSociete;
                    this.Document.DateCreation = DateTime.Now.Date;
                    this._pageContext.Documents.Add(this.Document);
                }
            }
        }
    }
}
